package bfs;

import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by qianqb on 2014/6/24.
 * 网页抓取的例子
 */
public class RetrivePage {

    private static HttpClient httpClient = HttpClients.createDefault();
    //设置代理服务器
    static {
        //设置代理服务器的IP地址和端口
        HttpHost host = new HttpHost("172.17.18.84",8080);
    }

    public static boolean downloadPage(String path) throws HttpException,IOException{
        InputStream input = null;
        OutputStream output = null;
        //得到Post方法
        HttpPost httpPost = new HttpPost(path);
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("username", "vip"));
        nvps.add(new BasicNameValuePair("password", "secret"));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps));
        CloseableHttpResponse response = (CloseableHttpResponse) httpClient.execute(httpPost);

//        Request.Get("http://targethost/homepage")
//                .execute().returnContent();
//        Request.Post("http://targethost/login")
//                .bodyForm(Form.form().add("username",  "vip").add("password",  "secret").build())
//                .execute().returnContent();



        try {

            StatusLine line = response.getStatusLine();
            while(line.getStatusCode() != HttpStatus.SC_OK){
                response = dealRedirection(response);
                line = response.getStatusLine();
            }
            dealOk(response);



        } finally {
            response.close();
        }
        return true;
    }

    public static void dealOk(HttpResponse response) throws IOException{
        StatusLine line = response.getStatusLine();
        if(line.getStatusCode() == HttpStatus.SC_OK){
            HttpEntity entity = response.getEntity();
            System.out.println(EntityUtils.toString(entity));
        }
        HttpEntity entity2 = response.getEntity();
        // do something useful with the response body
        // and ensure it is fully consumed
        EntityUtils.consume(entity2);
    }

    public static CloseableHttpResponse dealRedirection(CloseableHttpResponse response) throws IOException{
        StatusLine line = response.getStatusLine();
        int statusCode = line.getStatusCode();
        if((statusCode == HttpStatus.SC_MOVED_TEMPORARILY) || (statusCode == HttpStatus.SC_MOVED_PERMANENTLY)
                ||(statusCode == HttpStatus.SC_SEE_OTHER) || (statusCode == HttpStatus.SC_TEMPORARY_REDIRECT)){
            Header[] headers = response.getHeaders("location");
            if(headers[0] != null){
                String newUrl = headers[0].getValue();
                if(newUrl == null || newUrl.equals("")){
                    newUrl = "/";
                }
                //使用Post转向
                HttpPost redirect = new HttpPost(newUrl);
                response = (CloseableHttpResponse) httpClient.execute(redirect);
            }
        }
        return response;
    }

    public static void main(String[] args) {
        try {
            downloadPage("http://www.baidu.com");
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
